﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PhotoGallery.BLL;
using PhotoGallery.Model;
public partial class albumshow : System.Web.UI.Page
{
    protected string albumid="";
    protected string photolist = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request["aid"]==""||Request["aid"]==null)
            Response.Redirect("albumAll.aspx");
        if (Session["user"] == null) Response.Redirect("login.aspx");
        if (!IsPostBack) 
        {
            User u = (User)Session["user"];
            string aid = Request["aid"].ToString();
            this.albumid = aid;
            AlbumAction aa = new AlbumAction();
            PhotoAction pa = new PhotoAction();
            UserAction ua = new UserAction();
            //取得相册
            Albums album=aa.showAlbum(aid);
            string title = album.Title;//标题
            string author = album.Uid;//所属作者id
            string desp = album.Desp;//描述
            //取得封面图片
            Picture pic = pa.showCover(aid);
            string path = pic.Pic;//封面图片
            //取得作者信息
            string nickname = ua.getUserbyId(author).Nickname;
            string avator;
            Picture p = ua.getAvatorById(author);
            if (p != null)
            {
               avator = p.Pic;
            }
            else 
            {
                avator = "Images/user.png";
            }
            //控件赋值
            if (author == u.Uid)
            {
                this.owner.Visible = true;
                if (title == "头像相册")
                {
                    this.delete.Visible = false;
                    this.h11.Visible = false;
                    this.h12.Visible = false;
                }
                else
                {
                    this.h11.NavigateUrl = "picupload.aspx?aid=" + aid;
                    this.h12.NavigateUrl = "album.aspx?aid=" + aid;
                }
            }
            else
            {
                this.owner.Visible = false;
            }
            this.cover.ImageUrl = path;
            this.cover.Width = 120;
            this.albumtitle.Text = title;
            this.savt.ImageUrl = avator;
            this.author.Text = nickname;
            this.desp.Text = desp;
            //this.albumspacelink.NavigateUrl = "albumspace.aspx?uid=" + author;



            List<Picture> piclist = pa.showAllPhotosofAlbum(aid);
            if (piclist != null)
            {
                foreach (Picture mypic in piclist)
                {
                    photolist += "<div class='box showpic'><div class='picbox'><a href='photo.aspx?pid=" + mypic.Id + "' target='_blank'><img height='150' width='150' alt=" + mypic.Desp + " class='txpic' src=" + mypic.Pic + " style='display: block;'></a></div><p>" + mypic.Desp + "</p><div class='actions'><div class='lefter'><a class='button white' href='photo.aspx?pid=" + mypic.Id + "'>评论</a></div><div class='righter'><a class='button white' href='#'>收藏</a><a class='button white' href='#'>分享</a></div></div></div>";
                }
            }
            else
            {
                photolist = "暂无图片";

            }
        }
    }
    //判断是否有头像相册,如果有，则返回头像，如果没有，返回null
    private Picture getAvator(string uid)
    {
        AlbumAction aa = new AlbumAction();
        PhotoAction pa = new PhotoAction();
        List<Albums> alist = aa.allAlbumsofSomeone(uid);//取得某用户的所有相册
        if (alist.Count == 0) return null;//无相册，返回空
        //有相册
        foreach (Albums a in alist)
        {
            if (a.Title == "头像相册" && a.Desp == "头像相册")//有头像相册
            {
                return pa.showCover(a.Aid);
            }
            else //不是头像相册
            {

            }
        }
        return null;
    }
}